<template>
	<view>
		<diablog-shell ref="shell" title="用户登录" confirm-text="登 录" @confirm="confirmCommit">
			<image src="../../static/icons/icon.png" class="icon-login"></image>
			<text class="info-welcome">WELCOME</text>
			<text class="info-register" @click="clickRegister">没有账号？注册一个吧</text>
			<view class="login-container">
				<!-- <uni-easyinput v-model="username" placeholder="请输入用户名" /> -->
				<input type="text" v-model="username" placeholder="请输入用户名" placeholder-class="input-holder" @input="inputGetUser"/>
				<text class="iconfont icon-genshin_game_sign_materials is-user"></text>
			</view>
			<view class="login-container">
				<input v-if="isPwd" type="password" :value="password" placeholder="请输入密码" placeholder-class="input-holder" @input="inputGetPwd" />
				<input v-else type="text" :value="password" placeholder="请输入密码" placeholder-class="input-holder" @input="inputGetPwd" />
				<text :class="isPwd? 'iconfont icon-genshin_function_control_hide is-pwd':'iconfont icon-genshin_game_classify_plant2 is-pwd'" 
				@click="clickSwitch"></text>
			</view>

		</diablog-shell>
	</view>
</template>

<script>
	export default {
		name: "login-dailog",
		data() {
			return {
				username: "",
				password: "",
				isPwd: true,
			};
		},
		mounted() {
			let userinfo=uni.getStorageSync("userinfo")
			if(userinfo){
				this.username=userinfo.username,
				this.password=userinfo.password
			}
		},
		methods: {
			confirmCommit() {
				 if (this.username.length == 0 || this.password.length == 0) {
					uni.showToast({
						title: '请输用户名入密码！'
					});
					return
				}
				let url = this.$par.host + this.$par.action_login
				let data = {
					username: this.username,
					password: this.password
				}
				uni.showLoading({
					title: '正在登录中',
					mask: false
				});
				this.$req.post(url, data, res => {
					
					uni.showToast({
						title: res.data.message,
						icon: 'none'
					});
					if (!res.data.success) {
						this.$refs.shell.show()
						return
					}
					let app = getApp()
					let json = res.data.data
					app.globalData.avatar = json.avatar
					app.globalData.uid = json.id
					app.globalData.nickname = json.nickName
					app.globalData.username = json.userName
					app.globalData.token = json.token
					app.globalData.email = json.email
					app.globalData.type = json.type
					console.log("你已登录");
					//this.$noti.post(this.$par.notice_login_status)
					uni.setStorageSync("userinfo",data)
					this.$emit("after")
				}, () => {
					uni.hideLoading()
				})
			},
			clickRegister() {
				uni.navigateTo({
					url: '/pages/register/register',
					success: res => {
						this.$refs.shell.clickHide()
					},
					fail: () => {},
					complete: () => {}
				});
			},
			clickSwitch() {
				this.isPwd = !this.isPwd
			},
			inputGetUser(e) {
				this.username = e.detail.value
			},
			inputGetPwd(e) {
				this.password = e.detail.value
			},
			
			show() {
				this.$refs.shell.show()
			},
			hide() {
				this.$refs.shell.clickHide()
			},


		}

	}
</script>

<style>
	@import url("login-dailog.css");
</style>